<?php
namespace App\Models;

class ClientUserManagerDuty extends CommonModel {
	protected $table = 'client_user_manager_duty';
	
	// 开始值班
	public static function switch($uid){
		$date = date('Y-m-d');
		$working = static::where(['uid'=>$uid])->today()->value('status') ?? 0;

		if($working){
			$var = static::where(['uid'=>$uid])->today()->update(['status'=>0,'end_time'=>time()]);
		}else{
			$exists = static::where(['uid'=>$uid])->today()->exists();
			if($exists){
				$var = static::where(['uid'=>$uid])->today()->update(['status'=>1]);
			}else{
				$now_time = request()->server('REQUEST_TIME');
				$create = [
					'uid'		=> $uid,
					'date'		=> $date,
					'start_time'=> $now_time,
					'end_time'	=> $now_time,
					'status'	=> 1,
				];
				$var = static::create($create)->id;
			}
		}
		return $var;
	}
	
	
	// 值班状态
	public function scopeToday($query){
		$date = date('Y-m-d');
		$query->where(['date'=>$date]);
	}
}